<template>
  <div class="app-container">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>景点管理</span>
      </div>
      <div class="text item">
        <!-- 搜索区域 -->
        <el-form :inline="true" :model="queryParams" class="demo-form-inline">
          <el-form-item label="景点名称">
            <el-input v-model="queryParams.name" placeholder="请输入景点名称"></el-input>
          </el-form-item>
          <el-form-item label="景点等级">
            <el-select v-model="queryParams.level" placeholder="请选择景点等级" clearable>
              <el-option label="5A" value="5A"></el-option>
              <el-option label="4A" value="4A"></el-option>
              <el-option label="3A" value="3A"></el-option>
              <el-option label="2A" value="2A"></el-option>
              <el-option label="1A" value="1A"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleQuery">查询</el-button>
            <el-button v-if="checkPermission(['travel:scenic:add'])" type="success" @click="handleAdd">新增</el-button>
          </el-form-item>
        </el-form>

        <!-- 表格区域 -->
        <el-table :data="scenicList" style="width: 100%" v-loading="loading">
          <el-table-column prop="name" label="景点名称"></el-table-column>
          <el-table-column prop="level" label="景点等级" width="100"></el-table-column>
          <el-table-column prop="address" label="地址"></el-table-column>
          <el-table-column prop="price" label="门票价格" width="100">
            <template slot-scope="scope">
              ¥{{ scope.row.price }}
            </template>
          </el-table-column>
          <el-table-column prop="openTime" label="开放时间" width="180"></el-table-column>
          <el-table-column prop="status" label="状态" width="100">
            <template slot-scope="scope">
              <el-tag :type="scope.row.status === 1 ? 'success' : 'danger'">
                {{ scope.row.status === 1 ? '正常' : '关闭' }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="200">
            <template slot-scope="scope">
              <el-button v-if="checkPermission(['travel:scenic:edit'])" 
                type="primary" size="mini" @click="handleEdit(scope.row)">编辑</el-button>
              <el-button v-if="checkPermission(['travel:scenic:delete'])" 
                type="danger" size="mini" @click="handleDelete(scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>

        <!-- 分页区域 -->
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryParams.pageNum"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="queryParams.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
      </div>
    </el-card>

    <!-- 添加/编辑对话框 -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="景点名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入景点名称"></el-input>
        </el-form-item>
        <el-form-item label="景点等级" prop="level">
          <el-select v-model="form.level" placeholder="请选择景点等级">
            <el-option label="5A" value="5A"></el-option>
            <el-option label="4A" value="4A"></el-option>
            <el-option label="3A" value="3A"></el-option>
            <el-option label="2A" value="2A"></el-option>
            <el-option label="1A" value="1A"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入地址"></el-input>
        </el-form-item>
        <el-form-item label="门票价格" prop="price">
          <el-input-number v-model="form.price" :min="0" :precision="2" :step="0.1"></el-input-number>
        </el-form-item>
        <el-form-item label="开放时间" prop="openTime">
          <el-input v-model="form.openTime" placeholder="请输入开放时间"></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="form.status">
            <el-radio :label="1">正常</el-radio>
            <el-radio :label="0">关闭</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'Scenic',
  data() {
    return {
      // 遮罩层
      loading: false,
      // 总条数
      total: 0,
      // 景点表格数据
      scenicList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: '',
        level: ''
      },
      // 弹出层标题
      dialogTitle: '',
      // 弹出层是否显示
      dialogVisible: false,
      // 表单参数
      form: {
        id: undefined,
        name: undefined,
        level: undefined,
        address: undefined,
        price: 0,
        openTime: undefined,
        status: 1
      },
      // 表单校验
      rules: {
        name: [
          { required: true, message: '景点名称不能为空', trigger: 'blur' }
        ],
        level: [
          { required: true, message: '景点等级不能为空', trigger: 'change' }
        ],
        address: [
          { required: true, message: '地址不能为空', trigger: 'blur' }
        ],
        price: [
          { required: true, message: '门票价格不能为空', trigger: 'blur' }
        ],
        openTime: [
          { required: true, message: '开放时间不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    checkPermission(permissions) {
      // TODO: 实现权限检查
      return true
    },
    async getList() {
      this.loading = true
      try {
        // TODO: 调用景点列表API
        // 模拟数据
        this.scenicList = [
          {
            id: 1,
            name: '故宫博物院',
            level: '5A',
            address: '北京市东城区景山前街4号',
            price: 60,
            openTime: '8:30-17:00',
            status: 1
          },
          {
            id: 2,
            name: '长城',
            level: '5A',
            address: '北京市延庆区八达岭长城',
            price: 40,
            openTime: '6:30-19:00',
            status: 1
          }
        ]
        this.total = this.scenicList.length
        this.loading = false
      } catch (error) {
        this.loading = false
        console.error('获取景点列表失败:', error)
      }
    },
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    handleAdd() {
      this.dialogTitle = '添加景点'
      this.form = {
        id: undefined,
        name: undefined,
        level: undefined,
        address: undefined,
        price: 0,
        openTime: undefined,
        status: 1
      }
      this.dialogVisible = true
    },
    handleEdit(row) {
      this.dialogTitle = '编辑景点'
      this.form = { ...row }
      this.dialogVisible = true
    },
    handleDelete(row) {
      this.$confirm('确认删除该景点吗？', '提示', {
        type: 'warning'
      }).then(() => {
        // TODO: 调用删除API
        this.$message.success('删除成功')
        this.getList()
      }).catch(() => {})
    },
    submitForm() {
      this.$refs.form.validate(valid => {
        if (valid) {
          // TODO: 调用保存API
          this.$message.success(this.form.id ? '修改成功' : '新增成功')
          this.dialogVisible = false
          this.getList()
        }
      })
    },
    handleSizeChange(val) {
      this.queryParams.pageSize = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.queryParams.pageNum = val
      this.getList()
    }
  }
}
</script>

<style lang="scss" scoped>
.app-container {
  padding: 20px;
}
</style> 